﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace Card.Manager
{
    public partial class CityEdit : BasePage
    {
        #region
        private string Code
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["c"]))
                {
                    return Request.QueryString["c"].ToLower();
                }
                return string.Empty;
            }
        }

        private int Sort
        {
            get
            {
                int sort = 0;
                int.TryParse(this.txtSort.Text, out sort);
                return sort;
            }
        }
        #endregion
        #region
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DoInit();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.DoCheck())
            {
                BAF.Entity.LookupValue city = new BAF.Entity.LookupValue();
                city.LookupType = CONST.LOOKUP_TYPE.CITY;
                city.LookupCode = this.txtCode.Text;
                city.Value = this.txtName.Text;
                city.Sort = this.Sort;
                city.TextValue = this.txtURL.Text;
                city.Attribute1 = this.dropProvince.SelectedValue;
                city.Attribute2 = this.dropKey.SelectedValue;
                city.Attribute3 = this.dropIsActive.SelectedValue;
                if (BAF.BLL.LookupValueManager.Save(city, this.Passport))
                {
                    this.MessageBox("保存成功", string.Format("CityEdit.aspx?c={0}", city.LookupCode));
                }
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("CityManager.aspx?k={0}", this.dropKey.SelectedValue));
        }
        #endregion

        #region
        protected override void OnInit(EventArgs e)
        {
            this.userPicker.OnUserPicker += new Card.Manager.Controls.UserPicker.PickerUser(userPicker_OnUserPicker);
            base.OnInit(e);
        }

        void userPicker_OnUserPicker(int userID)
        {
            BLL.CityManagerMangager.Save(new Card.Entity.CityManager()
            {
                UserID = userID,
                CityCode = this.Code
            }, this.CPassport);

            this.DoBindManager();
        }
        private void DoInit()
        {
            DataTable dtProvince = BAF.BLL.LookupValueManager.GetListByLookUpType(CONST.LOOKUP_TYPE.PROVINCE, this.Passport);
            this.dropProvince.DataSource = dtProvince;
            this.dropProvince.DataTextField = "VALUE";
            this.dropProvince.DataValueField = "LOOKUP_CODE";
            this.dropProvince.DataBind();

            if (!string.IsNullOrEmpty(this.Code))
            {
                BAF.Entity.LookupValue city = BAF.BLL.LookupValueManager.GetEntity(CONST.LOOKUP_TYPE.CITY, this.Code, this.Passport);
                if (city != null)
                {
                    this.txtCode.Enabled = false;
                    this.txtCode.Text = city.LookupCode;
                    this.txtName.Text = city.Value;
                    this.txtURL.Text = city.TextValue;
                    this.txtSort.Text = city.Sort.ToString();
                    BAF.Utility.ControlHelper.SelectItemByValue(this.dropProvince, city.Attribute1);
                    BAF.Utility.ControlHelper.SelectItemByValue(this.dropKey, city.Attribute2);
                    BAF.Utility.ControlHelper.SelectItemByValue(this.dropIsActive, city.Attribute3);
                    this.DoBindManager();
                }
            }
        }

        private bool DoCheck()
        {
            if (string.IsNullOrEmpty(this.txtCode.Text))
            {
                this.MessageBox("请输入编码！");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                this.MessageBox("请输入名称！");
                return false;
            }
            if (string.Compare(this.dropIsActive.SelectedValue, "Y") == 0 && string.IsNullOrEmpty(this.txtURL.Text))
            {
                this.MessageBox("请输入网址！");
                return false;
            }
            if (this.txtCode.Enabled && BAF.BLL.LookupValueManager.Exists(CONST.LOOKUP_TYPE.CITY, this.txtCode.Text, this.Passport))
            {
                this.MessageBox("编码已存在！");
                return false;
            }
            return true;
        }

        private void DoBindManager()
        {
            this.trManager.Visible = true;
            DataTable dtData = BLL.CityManagerMangager.GetManagerByCityCode(this.Code, this.CPassport);
            this.userPicker.ExcludeUserIDs = string.Join(",", (from row in dtData.AsEnumerable()
                                                               select row["USER_ID"].ToString()).ToArray());

            this.rptManager.DataSource = dtData;
            this.rptManager.DataBind();
        }

        protected void rptManager_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DELETE")
            {
                BLL.CityManagerMangager.Delete(int.Parse(e.CommandArgument.ToString()),this.Code, this.CPassport);
            }
            this.DoBindManager();
        }
        #endregion



    }
}
